﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public partial class adduser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        if (Session["power"].ToString() != "超级管理员")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() == "" || TextBox2.Text.Trim() == "" || TextBox3.Text.Trim() == "" || TextBox4.Text.Trim() == "" || RadioButtonList1.SelectedValue == "" || TextBox5.Text.Trim() == "" || TextBox6.Text.Trim() == "" || DropDownList1.SelectedValue == "")
        {
            Response.Write("<script>alert('请填写完整信息!')</script>");
        }
        else if (TextBox2.Text != TextBox3.Text)
        {
            Response.Write("<script>alert('两次密码输入不一致!')</script>");
        }
        else
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlSTR = "select * from [Users] where UID='" + TextBox1.Text + "'";
            SqlCommand myCMD = new SqlCommand(sqlSTR, myConn);
            if (myCMD.ExecuteScalar() != null)
            {
                Response.Write("<script>alert('已存在该用户!')</script>");
            }
            else
            {
                string tt = MD5Manager5.Md5Encrypt(TextBox2.Text);
                string sqlStr = "insert into [Users](UID,UPassword,UPower,UName,USex,UTel,UAdress,UDep) values('" + TextBox1.Text + "','" + tt + "','" + DropDownList1.SelectedValue + "','" + TextBox4.Text + "','" + RadioButtonList1.SelectedValue + "','" + TextBox5.Text + "','" + TextBox6.Text + "','0')";
                SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
                myCmd.ExecuteNonQuery();
                myConn.Close();
                Response.Redirect("~/success.aspx?");
            }
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
}

class MD5Manager5
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strSource"></param>
    /// <returns></returns>
    public static string Md5Encrypt(string strSource)
    {
        byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };

        DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
        mobjCryptoService.IV = iv;
        mobjCryptoService.Key = key;

        ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
        cs.Write(bytIn, 0, bytIn.Length);
        cs.FlushFinalBlock();

        return System.Convert.ToBase64String(ms.ToArray());

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strSource"></param>
    /// <returns></returns>
    public static string Md5Decrypt(string strSource)
    {
        byte[] bytIn = System.Convert.FromBase64String(strSource);
        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };

        DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
        mobjCryptoService.IV = iv;
        mobjCryptoService.Key = key;

        ICryptoTransform decrypto = mobjCryptoService.CreateDecryptor();
        MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
        CryptoStream cs = new CryptoStream(ms, decrypto, CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cs, Encoding.Default);

        return sr.ReadToEnd();
    }
}